//
//  fGBussinessTableViewController.m
//  fGeekApp
//
//  Created by Administrator on 15/3/6.
//  Copyright (c) 2015年 cwx. All rights reserved.
//+-----------------------------------------------------------
// 商家模块：主要完成：1.tableView 信息展示 + 搜索排序信息
//                 2.搜索排序分为：按评论数量、购买数量、商品优惠价格
//                 3.商品购买
//+-----------------------------------------------------------
// 涉及的接口：“具体商品信息”展示接口 kHTTP/goods/
//       http://localhost:8888/fGeekApp/index.php/home/
//+-----------------------------------------------------------

#import "fGBussinessTableViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "Goods.h"
@interface fGBussinessTableViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *goodsIconView;
@property (weak, nonatomic) IBOutlet UILabel *goodsNameLable;
@property (weak, nonatomic) IBOutlet UILabel *goodsPriceLable;

@property(strong,nonatomic)Goods *currentGoods;
@end

@implementation fGBussinessTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"商家";
    [self requestGoodByID];
    
}


#pragma mark - 根据某一商品ID请求数据，异步请求－GET方式
-(void)requestGoodByID{
    //1.管理器
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    
    //2.设定类型. (这里要设置request-response的类型)
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    //3.获取url路径  http://localhost:8888/myShop/index.php/home/
    NSString *urlPath =[NSString stringWithFormat:@"%@goods/getgoods",kHTTP];
    
    //4.设置URL参数
//    NSDictionary *para = @{@"goodsid":@(self.goodsID)};
    NSDictionary *para = @{@"goodsid":@"1"};
    
    //    NSLog(@"%d",self.goodsID);
    
    [manager GET:urlPath parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //获取当前解析出来的数据
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        NSArray *dataArray = [responseDict valueForKey:@"data"];
        self.currentGoods = [Goods getGoodsInstanceByDict:[dataArray objectAtIndex:0]];
        
        self.goodsNameLable.text = self.currentGoods.goodsName;
        self.goodsPriceLable.text = [NSString stringWithFormat:@"$%.2f",self.currentGoods.goodsPrice];
        
        //使用AFN提供的UIImageView的扩展，异步加载每一行的图片
        //当数据库没图片的时候，默认给一张图片 default.png
        NSString *urlPath = [NSString stringWithFormat:@"%@%@",kHTTPImg,self.currentGoods.goodsImagePath];
        [self.goodsIconView setImageWithURL:[NSURL URLWithString:urlPath] placeholderImage:[UIImage imageNamed:@"default.png"]];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败，请检查网络!");
    }];
}


#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
//    // Return the number of sections.
//    return 0;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
//    // Return the number of rows in the section.
//    return 0;
//}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
